//////////////////////////////////////
//// ASKIMA - COLLABORATOR PROFILE
//// LANGUAGES MANAGEMENT FUNCTIONS
//////////////////////////////////////

function appendLanguageInput() {

	var newLineName = languagesLineIDPrefix + (++languagesLineID);
	// Add a new line compound by 2 inputs
	$('#languages_inputs')
			.append(
					"<span id='"
							+ newLineName
							+ "'><input class='languageName'/><input class='languageLevel'/><br/></span>");
	// Enable auto complete on the first input
	$('#' + newLineName + ' > .languageName').autocomplete({
		delay : 0,
		source : languages
	});

	// Creates a new object which will contains the couple
	// languageName/languageLevel
	selectedLanguages[newLineName] = new Object();

	return newLineName;
}

function removeLanguageInput() {

	if (languagesLineID <= 0) {
		return;
	}
	var oldLineName = languagesLineIDPrefix + (languagesLineID--);

	// Remove the line from the panel
	$('#languages_inputs > #' + oldLineName).remove();

	// Remove the pointed object
	delete selectedLanguages[oldLineName];

	return oldLineName;
}

/**
 * Show the panel in order to add languages
 */

function showLanguagesPanel() {

	$.getJSON("profileLanguagesToJSON", function(data) {
		if (data == false) {
			// A a first language input (by default)
			appendLanguageInput();
		} else {
			$.each(data, function(i, val) {
				var name = val.language.name;
				var level = val.level;
				var createdInputName = appendLanguageInput();
				$(
						'#languages_inputs > #' + createdInputName
								+ " > .languageName").val(name);
				$(
						'#languages_inputs > #' + createdInputName
								+ " > .languageLevel").val(level);
			});
		}
	});

	// Finally open the panel
	$("#add_language_panel").dialog({
		title : 'Modifier vos langues',
		width : 490,
		close : function(event, ui) {
			resetAddLanguagesDialog()
		}
	});
}

function resetAddLanguagesDialog() {

	// Clear the array which contains selected languages
	selectedLanguages = {};

	// Clear the content of the panel
	$('#languages_inputs').empty();

	// Reset the input counter
	languagesLineID = -1;

	// Re append the first input
	appendLanguageInput();
}

function submitAddLanguagesForm() {

	var lineName;
	// Iterate over the add languages panel children and retrieves their
	// contents
	for ( var i = 0; i <= languagesLineID; i++) {
		lineName = languagesLineIDPrefix + i;
		selectedLanguages[lineName].lang = $(
				'#' + lineName + ' > .languageName').val();
		selectedLanguages[lineName].level = $(
				'#' + lineName + ' > .languageLevel').val();
	}

	// Send data to the server
	$
			.getJSON(
					"updateProfileLanguages",
					{
						langs : JSON.stringify(selectedLanguages),
					},
					function(data) {
						if (data == true) {
							alert("Les langues associées au profile ont été modifiées avec succès !");
							window.location.reload();
						} else {
							alert("Un problème est survenu durant la modification des langues associées au profil !");
							resetAddLanguagesDialog();
						}
					});
}
